/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.parser;

import edu.bsu.android.gtv.apps.bconnected.objects.Category;
import edu.bsu.android.gtv.apps.bconnected.objects.Content;
import edu.bsu.android.gtv.apps.bconnected.objects.Course;
import edu.bsu.android.gtv.apps.bconnected.objects.Degree;
import edu.bsu.android.gtv.apps.bconnected.objects.Event;
import edu.bsu.android.gtv.apps.bconnected.objects.Housing;
import edu.bsu.android.gtv.apps.bconnected.objects.Organization;
import edu.bsu.android.gtv.apps.bconnected.objects.Person;
import edu.bsu.android.gtv.apps.bconnected.objects.Term;

import java.util.ArrayList;
import java.util.List;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

/**
 * Android SAX parser for the XML feeds from the .NET service handler.  Each class parses a specific feed and loads the 
 * appropriate object including:
 * {@link Category}
 * {@link Content}
 * {@link Course}
 * {@link Degree}
 * {@link Event}
 * {@link Housing}
 * {@link Organization}
 * {@link Person}
 * {@link Term}
 * 
 * Samples of the XML feeds from the service handler can be found under the root folder - labeled "sample xml".  Caching is handled
 * by the .NET application, both at the XML doc level and the database.  Depending on the type of data being returned, the XML doc
 * may be cached by the web server for up to 24 hours.  
 * 
 * @author Kyle Parker, University Computing Services
 */
public class AndroidSaxServiceParser extends BaseServiceParser {
	public AndroidSaxServiceParser(String url, List<String> querystringParms) {
		super(url, querystringParms);
	}

	public ArrayList<Category> parseCategory() {
		final Category currentCategory = new Category();
		final ArrayList<Category> categories = new ArrayList<Category>();
		
		RootElement root = new RootElement(ROOT);
		Element category = root.getChild(CATEGORY);

		category.setEndElementListener(new EndElementListener() {
			public void end()  {
				categories.add(currentCategory.copy());
			}
		});

		category.getChild(CATEGORY_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCategory.setCategoryID(body);
			}
		});

		category.getChild(CATEGORY_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCategory.setCategoryName(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return categories;
	}

	public ArrayList<Content> parseContent() {
		final Content currentContent = new Content();
		final ArrayList<Content> items = new ArrayList<Content>();

        RootElement root = new RootElement(ROOT);
        Element content = root.getChild(CONTENT);
        
        content.setEndElementListener(new EndElementListener() {
			public void end() {
				items.add(currentContent.copy());
			}
		}); 

        content.getChild(CATEGORY_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentContent.setCategoryID(body);
			}
		});

        content.getChild(CATEGORY_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentContent.setCategoryName(body);
			}
		});
		
        content.getChild(CONTENT_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentContent.setContentID(body);
			}
		});

        content.getChild(FULL_STORY_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentContent.setFullStoryURL(body);
			}
		});

        content.getChild(HEADLINE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentContent.setHeadline(body);
			}
		});

        content.getChild(TAGLINE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentContent.setTagline(body);
			}
		});

        content.getChild(THUMB_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentContent.setThumbURL(body);
			}
		});
		
		try {		
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return items;
	}
	
	public ArrayList<Course> parseCourse() {
		final Course currentCourse = new Course();
		final ArrayList<Course> courses = new ArrayList<Course>();
		
		RootElement root = new RootElement(ROOT);
		Element course = root.getChild(COURSE);

		course.setEndElementListener(new EndElementListener() {
			public void end() {
				courses.add(currentCourse.copy());
			}
		});

		course.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setBuildingDesc(body);
			}
		});

		course.getChild(COURSE_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setCourseDesc(body);
			}
		});

		course.getChild(COURSE_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setCourseNum(body);
			}
		});
		
		course.getChild(COURSE_REF_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setCourseRefNum(body);
			}
		});
		
		course.getChild(COURSE_WITH_TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setCourseWithTitle(body);
			}
		});
		
		course.getChild(CREDIT_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setCreditType(body);
			}
		});
		
		course.getChild(CR_NC_A).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setCrNcA(body);
			}
		});
		
		course.getChild(DAY_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setDayTime(body);
			}
		});

		course.getChild(DISCIPLINE_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setDisciplineCode(body);
			}
		});

		course.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setError(body);
			}
		});

		course.getChild(FINAL_EXAM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setFinalExam(body);
			}
		});

		course.getChild(FULL_TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setFullTitle(body);
			}
		});

		course.getChild(HOURS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setHours(body);
			}
		});

		course.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setLatitude(body);
			}
		});

		course.getChild(LOCATION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setLocation(body);
			}
		});
		
		course.getChild(LOCATION_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setLocationURL(body);
			}
		});
		
		course.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setLongitude(body);
			}
		});

		course.getChild(REPEAT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setRepeat(body);
			}
		});

		course.getChild(SECTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setSection(body);
			}
		});
		
		course.getChild(TERM_BEGIN_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setTermBeginDate(body);
			}
		});
		
		course.getChild(TERM_END_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setTermEndDate(body);
			}
		});

		course.getChild(TERM_FULL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setTermFullDesc(body);
			}
		});
		
		course.getChild(TERM_NUMBER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setTermNumber(body);
			}
		});
		
		course.getChild(TERM_YEAR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setTermYear(body);
			}
		});
		
		course.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentCourse.setTitle(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}

		return courses;
	}

	public ArrayList<Degree> parseDegree() {
		final Degree currentDegree = new Degree();
		final ArrayList<Degree> degrees = new ArrayList<Degree>();
		
		RootElement root = new RootElement(ROOT);
		Element degree = root.getChild(DEGREE);

		degree.setEndElementListener(new EndElementListener() {
			public void end()  {
				degrees.add(currentDegree.copy());
			}
		});

		degree.getChild(DEGREE_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentDegree.setDegreeType(body);
			}
		});

		degree.getChild(DEPARTMENT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentDegree.setDepartment(body);
			}
		});
		
		degree.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentDegree.setError(body); 
			}
		});

		degree.getChild(PROGRAM_OPTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentDegree.setProgramOption(body);
			}
		});

		degree.getChild(PROGRAM_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentDegree.setProgramType(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return degrees;
	}
		
	public ArrayList<Event> parseEvent()
	{
		final edu.bsu.android.gtv.apps.bconnected.objects.Event currentEvent = new Event();
		final ArrayList<Event> events = new ArrayList<Event>();
		
		RootElement root = new RootElement(ROOT);
		Element event = root.getChild(EVENT);

		event.setEndElementListener(new EndElementListener() {
			public void end() {
				events.add(currentEvent.copy());
			}
		}); 

		event.getChild(AUDIENCE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setAudience(body);
			}
		});

		event.getChild(BEGIN_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setBeginDate(body);
			}
		});

		event.getChild(BEGIN_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setBeginTime(body);
			}
		});

		event.getChild(BUILDING_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setBuildingCode(body);
			}
		});

		event.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setBuildingDesc(body);
			}
		});

		event.getChild(CALENDAR_EVENT_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setCalendarEventID(body);
			}
		});

		event.getChild(CALENDAR_EVENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setCalendarEventName(body);
			}
		});

		event.getChild(CONTACT_EMAIL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setContactEmail(body);
			}
		});

		event.getChild(CONTACT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setContactName(body);
			}
		});

		event.getChild(CONTACT_PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setContactPhone(body);
			}
		});

		event.getChild(COST).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setCost(body);
			}
		});

		event.getChild(END_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setEndDate(body);
			}
		});

		event.getChild(END_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setEndTime(body);
			}
		});

		event.getChild(EVENT_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setEventDesc(body);
			}
		}); 

		event.getChild(EVENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setEventName(body);
			}
		});

		event.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setLatitude(body);
			}
		});

		event.getChild(LOCATION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setLocation(body);
			}
		});

		event.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setLongitude(body);
			}
		}); 

		event.getChild(SHARE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setShareURL(body);
			}
		});

		event.getChild(TERM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setTermID(body);
			}
		});

		event.getChild(UNIVERSITY_CALENDAR_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentEvent.setUniversityCalendarID(body);
			}
		});
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return events;
	}
	
	public ArrayList<Housing> parseHousing() {
		final Housing currentHousing = new Housing();
		final ArrayList<Housing> info = new ArrayList<Housing>();
		
		RootElement root = new RootElement(ROOT);
		Element housing = root.getChild(HOUSING); 

		housing.setEndElementListener(new EndElementListener() {
			public void end() {
				info.add(currentHousing.copy());
			}
		});

		housing.getChild(COMPLEX_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setComplexDesc(body);
			}
		});

		housing.getChild(COMPLEX_FEATURES).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setComplexFeatures(body);
			}
		});

		housing.getChild(COMPLEX_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setComplexName(body);
			}
		});
		
		housing.getChild(CONTRACT_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setContractType(body);
			}
		});
		
		housing.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setError(body); 
			}
		});
		
		housing.getChild(FLOOR_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setFloorCode(body);
			}
		});
		
		housing.getChild(FLOOR_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setFloorDesc(body);
			}
		});
		
		housing.getChild(HALL_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setHallCode(body);
			}
		});
		
		housing.getChild(HALL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setHallDesc(body);
			}
		});

		housing.getChild(HALL_GROUP_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setHallGroupDesc(body);
			}
		});

		housing.getChild(IMAGE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setImageURL(body);
			}
		});

		housing.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setLatitude(body);
			}
		});

		housing.getChild(LIVING_LEARNING).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setLivingLearning(body);
			}
		});

		housing.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setLongitude(body);
			}
		});

		housing.getChild(MEAL_PLAN).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setMealPlan(body);
			}
		});

		housing.getChild(RATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setRate(body);
			}
		});
		
		housing.getChild(ROOM_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setRoomDesc(body);
			}
		});
		
		housing.getChild(ROOM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setRoomID(body);
			}
		});

		housing.getChild(ROOM_NUMBER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentHousing.setRoomNumber(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}

		return info;
	}
	
	public ArrayList<Organization> parseOrganization() {
		final Organization currentOrg = new Organization();
		final ArrayList<Organization> orgs = new ArrayList<Organization>();
		
		RootElement root = new RootElement(ROOT);
		Element org = root.getChild(ORGANIZATION);

		org.setEndElementListener(new EndElementListener() {
			public void end() {
				orgs.add(currentOrg.copy());
			}
		});

		org.getChild(ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setAddress(body);
			}
		});

		org.getChild(CATEGORY).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setCategory(body);
			}
		});

		org.getChild(DEGREE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setDegree(body);
			}
		});

		org.getChild(DISPLAY_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setDisplayName(body);
			}
		});

		org.getChild(DUES).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setDues(body);
			}
		});

		org.getChild(EMAIL_ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setEmailAddress(body);
			}
		});

		org.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setError(body);
			}
		});

		org.getChild(FOUNDED).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setFounded(body);
			}
		});
		
		org.getChild(MEMBER_COUNT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setMemberCount(body);
			}
		});
		
		org.getChild(MEMBERSHIP_REQ).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setMembershipReq(body);
			}
		});
		
		org.getChild(ORGANIZATION_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setOrganizationDesc(body);
			}
		});
		
		org.getChild(ORGANIZATION_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setOrganizationID(body);
			}
		});
		
		org.getChild(ORGANIZATION_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setOrganizationName(body);
			}
		});
		
		org.getChild(PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setPhone(body);
			}
		});
		
		org.getChild(PROGRAM_OPTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setProgramOption(body);
			}
		});

		org.getChild(PROGRAM_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setProgramType(body);
			}
		});

		org.getChild(SHARE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setShareURL(body);
			}
		});

		org.getChild(WEBSITE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentOrg.setWebsite(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}

		return orgs;
	}

	public ArrayList<Person> parsePerson() {
		final Person currentPerson = new Person();
		final ArrayList<Person> people = new ArrayList<Person>();
		
		RootElement root = new RootElement(ROOT);
		Element person = root.getChild(PERSON);

		person.setEndElementListener(new EndElementListener() {
			public void end() {
				people.add(currentPerson.copy());
			}
		});

		person.getChild(ADDRESS_1).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setAddress1(body);
			}
		});

		person.getChild(ADDRESS_2).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setAddress2(body);
			}
		});

		person.getChild(ADVISING_CENTER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setAdvisingCenter(body);
			}
		});

		person.getChild(ADVISOR_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setAdvisorName(body);
			}
		});

		person.getChild(ADVISOR_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setAdvisorType(body);
			}
		});

		person.getChild(BSU_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setBSUID(body);
			}
		});

		person.getChild(BUILDING_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setBuildingCode(body);
			}
		});

		person.getChild(BUILDING_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setBuildingDesc(body);
			}
		});

		person.getChild(CITY).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCity(body);
			}
		});
		
		person.getChild(COURSE_COUNT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseCount(body);
			}
		});

		person.getChild(COURSE_EMAIL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseEmail(body);
			}
		});
		
		person.getChild(COURSE_INFO).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseInfo(body);
			}
		});

		person.getChild(COURSE_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseNum(body);
			}
		});

		person.getChild(COURSE_REF_NUM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseRefNum(body);
			}
		});

		person.getChild(COURSE_URL).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseURL(body);
			}
		});

		person.getChild(COURSE_WITH_TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseWithTitle(body);
			}
		});

		person.getChild(DAY_TIME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setDayTime(body);
			}
		});

		person.getChild(DEPARTMENT).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setDepartment(body);
			}
		});

		person.getChild(DISCIPLINE_CODE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setDisciplineCode(body);
			}
		});

		person.getChild(EMAIL_ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setEmailAddress(body);
			}
		});

		person.getChild(ERROR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setError(body);
			}
		});

		person.getChild(FIRST_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setFirstName(body);
			}
		});

		person.getChild(LAST_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setLastName(body);
			}
		});

		person.getChild(LATITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setLatitude(body);
			}
		});

		person.getChild(LOGIN_GUID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setLoginGuid(body);
			}
		});

		person.getChild(LONGITUDE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setLongitude(body);
			}
		});

		person.getChild(MEMBER_ROLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setMemberRole(body);
			}
		});

		person.getChild(MEMBER_ROLE_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setMemberRoleID(body);
			}
		});

		person.getChild(MEMBER_TYPE_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setMemberTypeID(body);
			}
		});

		person.getChild(MIDDLE_NAME).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setMiddleName(body);
			}
		});

		person.getChild(NAME_PREFIX).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setNamePrefix(body);
			}
		});

		person.getChild(NAME_SUFFIX).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentPerson.setNameSuffix(body);
			}
		});

		person.getChild(OFFICE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body)  {
				currentPerson.setOffice(body);
			}
		});

		person.getChild(PERSON_TYPE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setPersonType(body);
			}
		});

		person.getChild(PHONE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setPhone(body);
			}
		});

		person.getChild(PRIVACY_RESTRICTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setPrivacyRestriction(body);
			}
		});

		person.getChild(ROLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setRole(body);
			}
		});

		person.getChild(ROOM).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setRoom(body);
			}
		});

		person.getChild(SECTION).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setSection(body);
			}
		});

		person.getChild(STATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setState(body);
			}
		});

		person.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setCourseTitle(body);
			}
		});

		person.getChild(TITLE_RANK).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setTitleRank(body);
			}
		});

		person.getChild(ZIP).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentPerson.setZip(body);
			}
		});
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return people;
	}
	
	public ArrayList<Term> parseTerm() {
		final Term currentTerm = new Term();
		final ArrayList<Term> terms = new ArrayList<Term>();
		
		RootElement root = new RootElement(ROOT);
		Element term = root.getChild(TERM);

		term.setEndElementListener(new EndElementListener() {
			public void end()  {
				terms.add(currentTerm.copy());
			}
		});

		term.getChild(END_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setEndDate(body);
			}
		});

		term.getChild(SHORT_ACADEMIC_YEAR).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setShortAcademicYear(body);
			}
		});

		term.getChild(SHORT_TERM_FULL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setShortTermFullDesc(body);
			}
		});

		term.getChild(START_DATE).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setStartDate(body);
			}
		});

		term.getChild(TERM_NUMBER).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setTermNumber(body);
			}
		});

		term.getChild(TERM_ID).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setTermID(body);
			}
		});

		term.getChild(TERM_FULL_DESC).setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				currentTerm.setTermFullDesc(body);
			}
		});
		
		try  {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}  catch (Exception e)  {
			throw new RuntimeException(e);
		}
		
		return terms;
	}
}
